unit Vars;

interface

uses
  Database,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls, Buttons, Menus, DB, Grids,
  ExtCtrls, DBTables, DBGrids;

type
  TFormVars = class(TForm)
    BtnDone: TBitBtn;
    BtnHelp: TBitBtn;
    MainMenu1: TMainMenu;
    Main1: TMenuItem;
    MenuHelp: TMenuItem;
    Bevel1: TBevel;
    Bevel2: TBevel;
    Bevel3: TBevel;
    EditVariable: TEdit;
    BtnAdd: TBitBtn;
    BtnDelete: TBitBtn;
    Filter: TLabel;
    EditInitialization: TEdit;
    Label1: TLabel;
    RBClear: TRadioButton;
    Bevel4: TBevel;
    RBSave: TRadioButton;
    RBValue: TRadioButton;
    Bevel5: TBevel;
    EditSet: TEdit;
    Label2: TLabel;
    BtnSet: TBitBtn;
    Bevel7: TBevel;
    Panel1: TPanel;
    Status: TLabel;
    BtnVars: TBitBtn;
    CtrlR1: TMenuItem;
    DSVariables: TDataSource;
    GridVariables: TDBGrid;
    procedure BtnDoneClick(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure BtnHelpClick(Sender: TObject);
    procedure BtnDeleteClick(Sender: TObject);
    procedure GridVariablesCalcCellColors(Sender: TObject; Field: TField;
      State: TGridDrawState; highlight: Boolean; AFont: TFont;
      ABrush: TBrush);
    procedure BtnAddClick(Sender: TObject);
    procedure EditVariableKeyPress(Sender: TObject; var Key: Char);
    procedure EditInitializationKeyPress(Sender: TObject; var Key: Char);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure GridVariablesDblClick(Sender: TObject);
    procedure GridVariablesKeyPress(Sender: TObject; var Key: Char);
    procedure RBClearClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure EditSetKeyPress(Sender: TObject; var Key: Char);
    procedure BtnSetClick(Sender: TObject);
    procedure BtnVarsClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
    nodelete: boolean;
  end;

var
  FormVars: TFormVars;

implementation

{$R *.DFM}

uses  VarGrabber, Main, DBClient, ScriptModule;

procedure TFormVars.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   Scripts.PrepareVars(datawell.TableVariables);
end;

procedure TFormVars.FormCreate(Sender: TObject);
begin
  nodelete:=false;
end;

procedure TFormVars.BtnDoneClick(Sender: TObject);
var table: TClientDataset;
begin
  GridVariables.hide;
  table:=TClientDataset.create(self);
  with table do begin
    cloneCursor(Datawell.TableVariables, false);
    filter := Datawell.TableVariables.filter;
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      FormMain.UpdateCanvasVariable('@'+fieldbyname('FVariable').asstring,fieldbyname('FValue').asstring);
      next;
    end;
  end;
  table.free;
  close;
end;

procedure TFormVars.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormVars.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('variables');
end;

procedure TFormVars.BtnDeleteClick(Sender: TObject);
var
  work: string;
  vars, events, groups: TStringList;
  i: integer;
begin
  if nodelete then begin
    messagedlg('You can only delete variables when this screen is opened from the main screen',
      mtinformation,[mbok],0);
    exit;
  end;
  if Datawell.TableVariables.EOF then exit;
  vars:=TStringList.create;
  groups:=TStringList.create;
  events:=TStringList.create;
  with Datawell.TableEvents do begin
    filter := 'Master = '+Datawell.masterindex;
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      work:=Datawell.TableEvents.fieldbyname('FAction').asstring;
      if copy(work,1,1) = '|' then system.delete(work,1,1);
      Datawell.Explode(work,',',vars);
      if vars.indexof(Datawell.TableVariables.fieldbyname('FVariable').asstring) >= 0 then begin
        groups.add(Datawell.TableEvents.fieldbyname('FGroup').asstring);
        events.add(Datawell.TableEvents.fieldbyname('FEvent').asstring);
      end;
      next;
    end;
  end;
  if events.count > 0 then begin
    work:='You cannot delete variables assigned to an event'+#13+
      'Event(s) that use this variable (Groups in []):';
    for i:=0 to events.count-1 do work:=work+#13+' ['+groups[i]+'] '+events[i];
    messagedlg(work,mtinformation,[mbok],0);
    exit;
  end;
  events.free;
  groups.free;
  vars.free;
  Datawell.TableVariables.delete;
  GridVariables.refresh;
end;

procedure TFormVars.GridVariablesCalcCellColors(Sender: TObject;
  Field: TField; State: TGridDrawState; highlight: Boolean; AFont: TFont;
  ABrush: TBrush);
begin
  if field.displayname = 'Variable' then begin
    afont.color:=Datawell.TableVariables.fieldbyname('FColor').asinteger;
    afont.style:=[fsbold];
    abrush.color:=Datawell.TableVariables.fieldbyname('BColor').asinteger;
  end
  else if field.displayname = 'Initialization' then begin
    if Datawell.TableVariables.fieldbyname('Initialize').asstring = '<clear on login>' then begin
      afont.color:=clgray;
      afont.style:=[fsbold,fsitalic];
      abrush.color:=clwhite;
    end
    else if Datawell.TableVariables.fieldbyname('Initialize').asstring = '<do not initialize>' then begin
      afont.color:=clwhite;
      afont.style:=[fsbold,fsitalic];
      abrush.color:=clgray;
    end
    else begin
      afont.color:=clblack;
      abrush.color:=clwhite;
    end;
  end;
end;

procedure TFormVars.EditVariableKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnAdd.click;
  end;
end;

procedure TFormVars.EditInitializationKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnAdd.click;
  end;
end;

procedure TFormVars.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key = VK_DELETE) and (activecontrol = GridVariables) then begin
    BtnDelete.click;
    key:=0;
  end;
end;

procedure TFormVars.GridVariablesDblClick(Sender: TObject);
var init: string;
begin
  if Datawell.TableVariables.EOF then exit;
  EditVariable.text:=Datawell.TableVariables.fieldbyname('FVariable').asstring;
  init:=Datawell.TableVariables.fieldbyname('Initialize').asstring;
  EditInitialization.text:='';
  if init = '<clear on login>' then RBClear.checked:=true
  else if init = '<do not initialize>' then RBSave.checked:=true
  else begin
    RBValue.checked:=true;
    EditInitialization.text:=init;
  end;
  EditSet.text:=Datawell.TableVariables.fieldbyname('FValue').asstring;
  EditSet.selectall;
  EditSet.setfocus;
end;

procedure TFormVars.BtnAddClick(Sender: TObject);
var
  didfind: boolean;
  failure: string;
  i: integer;
  table: TClientDataset;
begin
  if EditVariable.text = '' then begin
    messagedlg('You must enter a variable',mtinformation,[mbok],0);
    EditVariable.setfocus;
    exit;
  end;
  if not Datawell.ValidWordNumber(EditVariable.text) then begin
    messagedlg('Variables can contain only alphanumeric characters',
      mtinformation,[mbok],0);
    EditVariable.setfocus;
    exit;
  end;
  if Datawell.IsAlias(EditVariable.Text) then begin
    messagedlg('Variables can''t have the same name as an existing alias',
      mtinformation,[mbok],0);
    EditVariable.setfocus;
    exit;
  end;

  didfind := Datawell.TableVariables.Locate('FVariable', EditVariable.Text, [loCaseInsensitive]);
  if didfind then Datawell.TableVariables.edit
  else Datawell.TableVariables.append;
  Datawell.TableVariables.fieldbyname('Master').asinteger:=Datawell.masterinteger;
  Datawell.TableVariables.fieldbyname('FVariable').asstring:=EditVariable.text;
  if RBClear.checked then
    Datawell.TableVariables.fieldbyname('Initialize').asstring:='<clear on login>'
  else if RBSave.checked then
    Datawell.TableVariables.fieldbyname('Initialize').asstring:='<do not initialize>'
  else Datawell.TableVariables.fieldbyname('Initialize').asstring:=EditInitialization.text;
  Datawell.TableVariables.fieldbyname('FColor').asinteger:=clwhite;
  Datawell.TableVariables.fieldbyname('BColor').asinteger:=clnavy;
  Datawell.TableVariables.fieldbyname('Invisible').asboolean:=false;
  Datawell.TableVariables.post;
  EditVariable.text:='';
  EditInitialization.text:='';
  EditVariable.setfocus;
  GridVariables.refresh;
end;

procedure TFormVars.GridVariablesKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    GridVariablesDblClick(Sender);
  end;
end;

procedure TFormVars.RBClearClick(Sender: TObject);
begin
  if RBValue.checked then EditInitialization.color:=clwindow
  else EditInitialization.color:=clsilver;
  EditInitialization.readonly:=not RBValue.checked;
  EditInitialization.tabstop:=RBValue.checked;
end;

procedure TFormVars.EditSetKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnSet.click;
  end;
end;

procedure TFormVars.BtnSetClick(Sender: TObject);
begin
  if Datawell.TableVariables.fieldbyname('FVariable').asstring = '' then begin
    messagedlg('There is no variable to set',mtinformation,[mbok],0);
    exit;
  end;
  Datawell.TableVariables.edit;
  Datawell.TableVariables.fieldbyname('FValue').asstring:=EditSet.text;
  Datawell.TableVariables.post;
  GridVariables.refresh;
  EditSet.text:='';
  BtnDone.setfocus;
end;

procedure TFormVars.BtnVarsClick(Sender: TObject);
var form: TFormVarGrab;
begin
  form:=TFormVarGrab.create(self);
  form.setup;
  form.BtnVars.enabled:=false;
  form.BtnOK1.enabled:=false;
  form.BtnOK2.enabled:=false;
  form.BtnOK3.enabled:=false;
  form.BtnOK4.enabled:=false;
  //Disable Evaluators
  form.ListEvaluators.enabled:=false;
  form.ListEvaluators.color:=clsilver;
  form.Label4.enabled:=false;
  form.Label4a.enabled:=false;
  form.showmodal;
  form.free;
end;

end.
